#!/usr/bin/env python
#NMutl
"""Simple utilities and Vars - NO IMPORTS HERE !
"""
HTTP_DATE_FMT = "%a, %d %b %Y %H:%M:%S GMT"
rfc2822_DATE_FMT="%a, %d %b %Y %H:%M:%S +0000"
rfc2822_DATE_FMT="%a, %d %b %y %H:%M:%S +0000" 

def str_clip(s, maxLen):
    return s if len(s) <= maxLen+3 else s[:maxLen]+'...'
def tp_lastpart(strOrType):
    return str(strOrType).rsplit(' ')[-1].rsplit('.')[-1].strip("'>")
def dic_setFromLsts(dic, keyslst, vallst):
    for item in zip(keyslst, vallst):dic[item[0]]= item[1]  
    return dic
      
def ut_itemise(item,rt=[],incltypes=(str,unicode)):
    "must be called with empty rt=[]"
    if isinstance(item,dict):item=item.values()
    if isinstance(item,(list,tuple)):
        for it in item:ut_itemise(it,rt,incltypes)    
    elif isinstance(item,incltypes):rt.append(item)    
    return rt
def lst_break(bigList, maxLen):
    """returns  a list of lists  of maxLen""" 
    return [ bigList[i*maxLen:i*maxLen+maxLen] for i in xrange(len(bigList)/maxLen+int((len(bigList) % maxLen)>0) ) ]    
def lst_div(lst, numOfSubLists, pad=None):
    rt = [lst[start : start + numOfSubLists] for start in range(0, len(lst), numOfSubLists)]
    if pad is not None :
        rem = len(lst) % numOfSubLists
        rt[-1].extend ([pad] * rem) 
    return rt
def lst_removeEmpty(l):
    for it in l[:]: 
        if isinstance(it, list): 
            if len(it) == 0 :
                l.remove (it) 
            else:   
                lst_removeEmpty(it) 
    return l        
def lst_remove1Level0(l):
    for li in l: 
        if  isinstance(li, list):
            if len(li)==1 and isinstance(li[0], basestring): 
                l[l.index(li)]=li[0] 
            else: 
                lst_remove1Level0(li)    
def class_typeStr(obj):
    if not isinstance(obj, basestring):obj= str(type(obj))
    return obj.replace("'","").split(' ')[1][:-1]
def class_typeStrNoSuper(obj):
    return class_typeStr(obj).split('.')[-1]    
def dictFromQS(qs):
    return dict([ tuple(item.split('=')) for item in qs.split("&") if item!=''])

def fl_write(path, contents):
    fl=open(path, 'w')
    fl.write(contents)
    fl.close 
def fl_readlines(path):
    fin=open( path)
    rt=fin.readlines()
    fin.close()
    return rt 
def fl_read(path):
    fin=open( path)
    rt=fin.read()
    fin.close()
    return rt     
#def strReFindReplace(rawstr,replaceStr,strToSearch,count = 0):
#    r0=re.findall(rawstr ,strToSearch)
#    r1=re.subn(rawstr, replaceStr, strToSearch,count)
#    return(r0,r1[0],r1[1])                                              #([found strings],String after replacement,howMany Substituted)
 
#    NMmisc.os.path.expanduser("~")+'/'
    
#    recompiletype = NMmisc.re.compile("<class.*?\.|<.*?\s|'|>",  NMmisc.re.VERBOSE)
